import { app, ipcMain, IpcMainInvokeEvent } from 'electron';

import { exec, execFile } from 'child_process';

import IpcMessage from '../../preload/preload';

export default class ProcessIpcService {
  /**
   * Sets up all related ipc message listeners.
   */
  static setup(): void {
    // One-way communications.
    this.exitApp();
    this.exec();
    this.execFile();
  }

  private static exitApp(): void {
    ipcMain.on(IpcMessage.exitApp, () => app.exit());
  }

  private static exec(): void {
    ipcMain.on(IpcMessage.exec, (event: IpcMainInvokeEvent, fileFullName: string) =>
      exec(fileFullName),
    );
  }

  private static execFile(): void {
    ipcMain.on(IpcMessage.execFile, (event: IpcMainInvokeEvent, fileFullName: string) =>
      execFile(fileFullName),
    );
  }
}
